package cn.cs.service.impl;

import cn.cs.client.RestAfterOrderClient;
import cn.cs.client.RestClientUserClient;
import cn.cs.client.RestRepairUserClient;
import cn.cs.common.*;
import cn.cs.pojo.AfterOrder;
import cn.cs.pojo.ClientUser;
import cn.cs.pojo.RepairUser;
import cn.cs.service.AfterOrderService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AfterOrderServiceImpl implements AfterOrderService{
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private RestAfterOrderClient afterOrderClient;
    @Resource
    private RestClientUserClient clientUserClient;

    @Resource
    private RestRepairUserClient repairUserClient;

    @Override
    public Dto getAllAfterOrder(HttpServletRequest request) {
        String token=request.getHeader("token");
        RepairUser user = JSON.parseObject(redisUtils.getString(token), RepairUser.class);
        if(!EmptyUtils.isEmpty(user)){
            String state= request.getParameter("state");
            Map<String,Object> param = new HashMap<String, Object>();
            List<AfterOrder> list = null;
            param.put("repairUserId",user.getId());
            if(EmptyUtils.isNotEmpty(state)){
                param.put("state",state);
            }
            list  = afterOrderClient.getAfterOrderList(param);
            if(EmptyUtils.isEmpty(list)){
                return  DtoUtil.returnFail("","002");
            }else{
                return  DtoUtil.returnDataSuccess(list);
            }
        }else{
            return  DtoUtil.returnFail("用户未登录","001");
        }
    }

    @Override
    public Dto confirm(HttpServletRequest request) throws Exception{
        String token=request.getHeader("token");
        RepairUser user = JSON.parseObject(redisUtils.getString(token), RepairUser.class);
        if(!EmptyUtils.isEmpty(user)){
            String afterId= request.getParameter("id");
            String state =request.getParameter("state");
            AfterOrder order = new AfterOrder();
            if("1".equals(state)){
                order.setRepairUserDate(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            }else{
                order.setHandleDate(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            }
            order.setId(afterId);
            order.setState(state);
            int n = afterOrderClient.updateAfterOrder(order);
            if(n>0){
                return  DtoUtil.returnSuccess();
            }else{
                return  DtoUtil.returnFail("更改失败","002");
            }
        }else{
            return  DtoUtil.returnFail("用户未登录","001");
        }
    }

    @Override
    public Dto readShMsg(HttpServletRequest request) {
        String token=request.getHeader("token");
        RepairUser user = JSON.parseObject(redisUtils.getString(token), RepairUser.class);
        if(!EmptyUtils.isEmpty(user)){
            String afterId= request.getParameter("id");
            AfterOrder order = new AfterOrder();
            order.setId(afterId);
            order.setIsread("1");
            int n = afterOrderClient.updateAfterOrder(order);
            if(n>0){
                return  DtoUtil.returnSuccess();
            }else{
                return  DtoUtil.returnFail("更改失败","002");
            }
        }else{
            return  DtoUtil.returnFail("用户未登录","001");
        }
    }



    /**
     * 根据订单号，用户Id查询售后单
     * @param orderId
     * @param userId
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> getAfterOrderByUserIdAndOrderId(String orderId, String userId) throws Exception {
        return afterOrderClient.getAfterOrderByUserIdAndOrderId(orderId);
    }

    public List<Map<String, Object>> getAfterOrderByUserId(String orderId, String userId, String id) throws Exception {
        return afterOrderClient.getAfterOrderByUserId(orderId, userId, id);
    }

    public Integer addAfterOrder(AfterOrder afterOrder) throws Exception {
        return afterOrderClient.addAfterOrder(afterOrder);
    }

    public Integer modifyAfterOrder(AfterOrder afterOrder) throws Exception {
        return afterOrderClient.updateAfterOrder(afterOrder);
    }

    public Page<Map<String, Object>> queryAfterOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = afterOrderClient.getAfterOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String, Object>> afterOrderList = afterOrderClient.getAfterOrderListPage(param);
        for(Map<String, Object> a:afterOrderList){
            String userId = a.get("userId").toString();
            String repairUserId = a.get("repairUserId").toString();
            ClientUser clientUser = clientUserClient.getClientUserById(userId);
            a.put("userName", clientUser.getUserName());a.put("userHeadimg", clientUser.getHeadimg());a.put("userPhone", clientUser.getPhone());

            RepairUser repairUser = repairUserClient.getRepairUserById(repairUserId);
            a.put("repairPhone", repairUser.getPhone());a.put("personName", repairUser.getPersonName());

            String order_date = a.get("orderDate").toString();
            String order_time = String.format("%02d", Integer.valueOf(a.get("orderTime").toString().replace(" ", "").split(":")[0]));
            String date = order_date + " " + order_time + ":00:00";
            Double time = DateUtil.getMinuteOfTwoDate(new Date(), DateUtil.parse(date, "yyyy-MM-dd HH:mm:ss"));
            if(time >= 0){
                a.put("overTime", "1");
            }else{
                a.put("overTime", "0");
            }
        }
        page.setRows(afterOrderList);
        return page;
    }

    public Page<Map<String, Object>> getAfterOrderAllListPage(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = afterOrderClient.getAfterOrderAllCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String, Object>> afterOrderList = afterOrderClient.getAfterOrderAllListPage(param);
        for(Map<String, Object> a:afterOrderList){
            String userId = a.get("userId").toString();
            String repairUserId = a.get("repairUserId").toString();
            ClientUser clientUser = clientUserClient.getClientUserById(userId);
            a.put("userName", clientUser.getUserName());a.put("userHeadimg", clientUser.getHeadimg());a.put("userPhone", clientUser.getPhone());

            RepairUser repairUser = repairUserClient.getRepairUserById(repairUserId);
            a.put("repairPhone", repairUser.getPhone());a.put("personName", repairUser.getPersonName());
        }
        page.setRows(afterOrderList);
        return page;
    }


    public AfterOrder getAfterOrderByOrderIdOne( String orderId) throws Exception {
        return afterOrderClient.getAfterOrderByOrderIdOne(orderId);
    }
}
